package com.ibeeking.found.cop.b.rest.controller.v1;

import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.constants.GlobalConstant;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.common.enums.LoginRoleEnum;
import com.ibeeking.found.cop.b.rest.service.ICopSourceService;
import com.ibeeking.found.cop.service.common.param.CopSourceAddParam;
import com.ibeeking.found.cop.service.common.param.CopSourceModifyBatch;
import com.ibeeking.found.cop.service.common.param.CopSourceParam;
import com.ibeeking.found.cop.service.common.query.CopSourcePageQuery;
import com.ibeeking.found.cop.service.common.query.CopSourceQuery;
import com.ibeeking.found.cop.service.common.vo.CopSourceVO;
import com.ibeeking.found.cop.service.common.vo.CopSourceViewVo;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.base.BaseValidationGroup;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName CopSourceController
 * @Description
 * @Author ibeeking
 * @Date 2021-02-05 16:17
 **/
@RestController
@RequestMapping("/b/copSource/v1")
public class CopSourceController {

    @Resource
    private ICopSourceService copSourceServiceImpl;

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询素材")
    @PostMapping(value = "/list")
    public List<CopSourceVO> list(@RequestBody @Validated(BaseValidationGroup.Update.class) CopSourceQuery query) {
        return copSourceServiceImpl.list(query);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "分页查询素材")
    @PostMapping(value = "/pageList")
    public Page<CopSourceVO> pageList(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Update.class) CopSourcePageQuery pageQuery) {
        if(LoginRoleEnum.SHOP.name().equals(currentUser.getLoginSystem())){
            pageQuery.setShopId(currentUser.getCurrentShop().getShopId());
            pageQuery.setMerchantsBusiness(1);
        }else {
            pageQuery.setMerchantsBusiness(2);
        }
        return copSourceServiceImpl.pageList(pageQuery);
    }

        @Log(logType = OperateLogTypeEnum.SAVE, describe = "添加素材")
	@PostMapping(value = "/add")
	public Boolean add(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Insert.class) CopSourceAddParam param) {
        if(LoginRoleEnum.SHOP.name().equals(currentUser.getLoginSystem())){
            param.setShopId(currentUser.getCurrentShop().getShopId());
            param.setMerchantsBusiness(1);
        }else {
            param.setShopId(GlobalConstant.SHOP_ID_ZERO);
            param.setMerchantsBusiness(2);
        }
            param.setCreateBy(currentUser.getId());
            param.setModifyBy(currentUser.getId());
		return copSourceServiceImpl.add(param);
	}

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "修改素材")
    @PostMapping(value = "/modify")
    public Boolean modify(@LoginUser CurrentUser currentUser, @RequestBody @Validated(BaseValidationGroup.Update.class) CopSourceParam param) {
        param.setModifyBy(currentUser.getId());
        return copSourceServiceImpl.modify(param);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询素材详情")
    @GetMapping(value = "/view/{id}")
    public CopSourceViewVo view(@PathVariable("id") Long id) {
        return copSourceServiceImpl.view(id);
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "删除素材")
    @GetMapping(value = "/del/{id}")
    public Boolean del(@PathVariable("id") Long id) {
        return copSourceServiceImpl.del(id);
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "批量删除素材")
    @PostMapping(value = "/delBatch")
    public Boolean delBatchIds(@RequestBody CopSourceModifyBatch param) {
        return copSourceServiceImpl.delBatchIds(param.getIds());
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "移动到其它分组")
    @PostMapping(value = "/modifyBatch")
    public Boolean modifyBatchIds(@RequestBody @Validated(BaseValidationGroup.Insert.class) CopSourceModifyBatch param) {
        return copSourceServiceImpl.modifyBatchIds(param);
    }
}
